package com.tengju.data.infrastructure.repository.clickhouse.user;

import com.tengju.data.domain.model.clickhouse.DwdUserClubMapRepository;
import com.tengju.data.domain.model.clickhouse.LevelUserAnalysis;
import com.tengju.data.domain.model.user.UserBelongDetail;
import com.tengju.data.domain.model.user.UserDetail;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.LevelUserAnalysisVO;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.UserBelongDetailDO;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.UserDetailDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.user.DwdUserClubMapDOMapper;
import com.tengju.data.infrastructure.repository.clickhouse.convert.OdsUserInfoWithClubConvert;
import com.tengju.data.infrastructure.repository.clickhouse.convert.UserClubConvert;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DwdUserClubMapRepositoryImpl implements DwdUserClubMapRepository {

    private final DwdUserClubMapDOMapper dwdUserClubMapDOMapper;

    @Override
    public int getUserBlackCountByClubId(Long clubId) {
        return dwdUserClubMapDOMapper.selectUserBlackCountByClubId(clubId);
    }

    @Override
    public List<Long> getIdCodesByClubIdAndIdCode(Long clubId, List<Long> idCodes) {
        return dwdUserClubMapDOMapper.selectIdCodesByClubIdAndIdCode(clubId, idCodes);
    }

    @Override
    public List<Long> getIdCodesByClubIdsAndIdCodes(List<Long> clubIds, List<Long> idCodes) {
        return dwdUserClubMapDOMapper.selectIdCodesByClubIdsAndIdCodes(clubIds, idCodes);
    }

    @Override
    public List<LevelUserAnalysis> listLevelUsers(Long clubId) {
        List<LevelUserAnalysisVO> levelUserAnalysisVOList = dwdUserClubMapDOMapper.listLevelUsers(clubId);
        if (CollectionUtils.isEmpty(levelUserAnalysisVOList)) {
            return Collections.emptyList();
        }
        return levelUserAnalysisVOList.stream()
                .map(UserClubConvert::toLevelUserModel)
                .collect(Collectors.toList());
    }

    @Override
    public UserDetail queryUserDetail(UserIdCode idCode) {
        UserDetailDO userDetailDO = dwdUserClubMapDOMapper.selectUserDetails(idCode.getValue());
        return OdsUserInfoWithClubConvert.convertToModel(userDetailDO);
    }

    @Override
    public List<UserBelongDetail> queryUserBelongDetailList(List<UserIdCode> idCodeList) {
        List<Long> collect = idCodeList.stream().map(UserIdCode::getValue).collect(Collectors.toList());
        List<UserBelongDetailDO> list = dwdUserClubMapDOMapper.selectUserBelongDetailList(collect);
        return list.stream()
                .map(OdsUserInfoWithClubConvert::convertToModel)
                .collect(Collectors.toList());
    }

}
